package flex.cc.fenfa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;

import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.basicinformation.entity.ChannelEntity;
import flex.cc.basicinformation.entity.ServiceProviderEntity;
import flex.cc.basicinformation.service.ChannelService;
import flex.cc.basicinformation.service.ServiceProviderService;
import flex.cc.core.support.BaseController;
import flex.cc.fenfa.dto.RuleMainEntity;
import flex.cc.fenfa.dto.RuleServicerEntity;
import flex.cc.fenfa.service.RuleMainService;
import flex.cc.fenfa.service.RuleServicerService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @author WANGCONG
 * 2021年10月12日上午10:16:18
 * @desc 分发规则控制层
 */
@RestController
@RequestMapping(value = "/fenfa/manage")
public class FenFaController extends BaseController {

	@Resource
    private RuleMainService ruleMainService;
	@Resource
    private RuleServicerService servicerService;
	@Resource
	private ChannelService channelService;
	@Resource
	private ServiceProviderService serviceProviderService;
	
	/**
	 *
	 * @author WANGCONG
	 * 2021年10月12日上午10:16:55
	 * @desc 分发规则分页查询
	 */
	@PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询服务单列表")
    public Wrapper<IPage<RuleMainEntity>> queryOrderListWithPage(@ApiParam(name = "orderMain", value = "分发信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("查询分发规则列表fenfaMain={}", reqVo);
        RuleMainEntity ruleDto = new RuleMainEntity();
        ruleDto.setPageNum(Integer.parseInt(reqVo.get("pageNum").toString()));
        ruleDto.setPageSize(Integer.parseInt(reqVo.get("pageSize").toString()));
        IPage<RuleMainEntity> page = ruleMainService.queryListWithPage(ruleDto);
        return WrapMapper.ok(page);
    }
	
	/**
	 *
	 * @author WANGCONG
	 * 2021年10月12日下午1:08:36
	 * @desc 根据ruleId切换规则状态
	 */
    @PostMapping(value = "/modifyRuleStatus")
    @ApiOperation(httpMethod = "POST", value = "根据ruleId切换规则状态")
    public Wrapper<Boolean> modifyRuleStatus(@ApiParam(name = "modifyUserStatusDto", value = "规则启用/禁用")
                                                 @RequestBody RuleMainEntity ruleDto) {
        logger.info("根据ruleId切换规则状态ruleDto={}", ruleDto);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        boolean result = ruleMainService.modifyRuleStatus(ruleDto, loginAuthDto);
        return handleResult(result);
    }
    
    /**
	 * 
	 * @author WANGCONG
	 * 2021年10月13日上午9:05:08
	 * @desc 规则信息保存
	 */
    @PostMapping(value = "/save")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "规则保存")
    public Wrapper<String> save(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("分发规则保存reqVo={}", reqVo);
        ruleMainService.save(reqVo);
        return WrapMapper.ok();
    }
    
    /**
	 * 
	 * @author WANGCONG
	 * 2021年10月15日下午2:16:52
	 * @desc 规则信息详情
	 */
    @PostMapping(value = "/detail/{ruleId}")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "规则详情")
    public Wrapper<String> detail(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String ruleId) {
        logger.info("规则详情FenFaController -- detail() ====> ", ruleId);
        Map<String, Object> result = new HashMap<String, Object>();
        if (PublicUtil.isNotEmpty(ruleId)){
        	Map<String, Object> responseMsg = new HashMap<String, Object>();
        	RuleMainEntity mainVo = ruleMainService.queryByRuleId(ruleId);
        	List<RuleServicerEntity> servicerList = servicerService.queryListByRuleId(ruleId);
        	responseMsg.put("0", mainVo);
            responseMsg.put("1", servicerList);
            result.put("responseCode", "00");
            result.put("responseMsg", responseMsg);
            return WrapMapper.ok(JSON.toJSONString(result));
        }
        return WrapMapper.ok();
    }
    
    /**
	 * 
	 * @author WANGCONG
	 * 2021年10月18日下午1:32:36
	 * @desc 规则名称唯一性校验
	 */
    @PostMapping(value = "/checkRuleName")
    @ApiOperation(httpMethod = "POST", value = "校验规则名称唯一性")
    public Wrapper<String> checkRuleName(@ApiParam(name = "reqVo", value = "参数") @RequestBody RuleMainEntity ruleMainVo) {
    	logger.info("规则名称唯一性校验. ruleMainVo= {}", ruleMainVo);
    	int count = ruleMainService.findCountByRuleName(ruleMainVo);
    	return WrapMapper.ok(count+"");
    }
	
    @PostMapping(value = "/basicData")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "获取渠道商、服务商列表")
    public Wrapper<String> basicData() {
        logger.info("获取渠道商、服务商列表FenFaController -- basicData()");
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> responseMsg = new HashMap<String, Object>();
        Map<String, String> channelMap = new HashMap<>();
		List<ChannelEntity> channelList = channelService.findChannelByParam(channelMap);
		for(int i=0; i<channelList.size(); i++){
			ChannelEntity channelEntity = channelList.get(i);
			channelEntity.setClientId(channelEntity.getId()+"");
		}
        responseMsg.put("0", channelList);
        List<ServiceProviderEntity> servicerList = serviceProviderService.findList();
        for(int i=0; i<servicerList.size(); i++){
        	ServiceProviderEntity serviceEntity = servicerList.get(i);
        	serviceEntity.setClientId(serviceEntity.getId()+"");
		}
        responseMsg.put("1", servicerList);
        result.put("responseCode", "00");
        result.put("responseMsg", responseMsg);
        return WrapMapper.ok(JSON.toJSONString(result));
    }

    /**
     * @description 根据救援商id查询对应的规则是否为空或者0%，用于禁用救援商时判断【2.2-服务包配置需求】
     * @param serviceId 救援商id
     * @return
     * @author wkangle
     * @time 2022/03/07 0007 10:07
     * @exception/throws
    */
    @PostMapping(value = "/queryRuleByServiceId/{serviceId}")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "根据救援商id查询对应的规则是否为空或者0%，用于禁用救援商时判断【2.2-服务包配置需求】")
    public Wrapper<String> queryRuleByServiceId(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String serviceId) {
        logger.info("禁用救援商时判断其对应的分发规则 FenFaController -- queryRuleByServiceId() ====> {}", serviceId);
        Map<String, Object> result = new HashMap<String, Object>();
        if (PublicUtil.isNotEmpty(serviceId)){
            String ruleNames = ruleMainService.queryRuleNamesByServiceId(serviceId);
            return WrapMapper.ok(ruleNames);
        }
        return WrapMapper.ok();
    }
}
